package model

import (
	"fmt"
	sferrors "lib/sf-errors"
	"strings"
	"time"
)

type AgencyLoginReq struct {
	Email    string `json:"email" binding:"required" example:"admin@simplifly.tech"`
	Password string `json:"password" binding:"required" example:"123456"`
	Code     string `json:"code" binding:"required" example:"6688"`
}

type LoginRequest struct {
	Mobile   string `json:"mobile" binding:"required" example:"132****8999"`
	Password string `json:"password" binding:"required" example:"123456"`
}
type MobileLoginRequest struct {
	Mobile  string `json:"mobile" binding:"required" example:"132****8999"`
	Captcha string `json:"captcha" binding:"required" example:"123456"`
}
type CreateAgencyApplicationRequest struct {
	Name        string `json:"name" binding:"required" example:"Brice"`
	Nationality string `json:"nationality" example:"CN"`
	Email       string `json:"email" binding:"required" example:"brice@example.com"`
	Region      string `json:"region" example:"86"`
	Phone       string `json:"phone" example:"1345678901"`
	Company     string `json:"company"  binding:"required" example:"Simplifly Co., Ltd."`
	Website     string `json:"website" example:"https://www.simplifly.com"`
	Description string `json:"description" binding:"required" example:"We are a startup travel agency"`
	Address     string `json:"address" binding:"required" example:"北京市朝阳区XX街道XX号"`
}

type ChangePasswordRequest struct {
	Code     string `json:"code" binding:"required" example:"123456"`
	Password string `json:"password" binding:"required" example:"123456"`
}

type ChangeUserAvatar struct {
	Avatar string `json:"avatar" binding:"required" example:"avatar url"`
}

type UpdateMeRequest struct {
	Nickname string `json:"nickname" example:"今晚打老虎"`
	Region   string `json:"region" example:"1"`
	Mobile   string `json:"mobile" example:"13213047911"`
	Avatar   string `json:"avatar" example:"avatar url"`
}

type QueryUserLogRequest struct {
	Start     string `json:"start" form:"start" extensions:"x-nullable"`
	End       string `json:"end" form:"end" extensions:"x-nullable"`
	Type      int    `json:"type" form:"type" extensions:"x-nullable"`
	Indexs    int    `json:"indexs" form:"indexs" extensions:"x-nullable"`
	OrderType int    `json:"orderType" form:"orderType" extensions:"x-nullable"`
	PublicSearchData
}

type QueryUserLogRequestss struct {
	Start     string `json:"start" form:"start" extensions:"x-nullable"`
	OrderType int    `json:"orderType" form:"orderType" extensions:"x-nullable"`
}

func checkTime(times string) error {
	_, err := time.Parse("2006-01-02 15:04:05", times)
	if err != nil {
		return sferrors.New(err).SetCode(sferrors.InvalidParameter)
	}
	return nil
}

func (q *QueryUserLogRequest) Get() (string, []any, error) {
	queryStr, queryParams := "", make([]any, 0, 4)
	if q.Start != "" {
		if err := checkTime(q.Start); err != nil {
			return "", nil, err
		}

		queryStr = fmt.Sprintf("%s and created_at >= ?", queryStr)
		queryParams = append(queryParams, q.Start)
	}
	if q.End != "" {
		if err := checkTime(q.End); err != nil {
			return "", nil, err
		}
		queryStr = fmt.Sprintf("%s and created_at<= ?", queryStr)
		queryParams = append(queryParams, q.End)
	}

	if q.Query != "" {
		queryStr = fmt.Sprintf("%s and ip like ? or action like ?", queryStr)
		queryParams = append(queryParams, "%"+q.Query+"%", "%"+q.Query+"%")
	}

	queryStr = strings.TrimSpace(queryStr)
	queryStr = strings.TrimLeft(queryStr, "and ")
	return queryStr, queryParams, nil
}

type SupplierRequest struct {
	Email    string `json:"email" binding:"required" example:"admin@simplifly.tech"`
	Password string `json:"password" binding:"required" example:"123456"`
	Code     string `json:"code" binding:"required" example:"6688"`
}
type UserRank struct {
	Uid     string `json:"uid"`
	Cnt     int    `json:"cnt"`
	Ranking int    `json:"ranking"`
}
